package yxy.game.pm2.command.yilingbattle;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.BattleMessage;
import yxy.game.pm2.message.YiLingMessage;
import yxy.game.pm2.module.yiling.YiLingBattleHandle;

/****************************************
 * @name CMD_YiLingBattleEnemyExecute
 * @description 夷陵之战 战斗开始
 * @author 梁健彬
 * @since 2022-05-17
 ***************************************/
@MessageType(proto = Command.YiLingBattleEnemyExecute, classtype = YiLingMessage.YiLingBattleEnemyExecuteRequest.class)
public class CMD_YiLingBattleEnemyExecute extends Command<YiLingMessage.YiLingBattleEnemyExecuteRequest> {

    @Override
    public void execute(MessageSource messageSource, YiLingMessage.YiLingBattleEnemyExecuteRequest request) {

        YiLingMessage.YiLingBattleEnemyExecuteResponse.Builder response = YiLingMessage.YiLingBattleEnemyExecuteResponse.newBuilder();

        try {
            String playerCode = messageSource.getTag();
            BattleMessage.BattleExecuteRequest battleReq = request.getBattleReq();
            YiLingBattleHandle handle = new YiLingBattleHandle();
            if (handle.isBattling(playerCode)) {
                send(messageSource, response, Result.BattleIsRunning);// 还有正在进行中的战斗
                return;
            }

            //返回战斗记录
            response.setBattleResp(handle.battleResponseExecute(playerCode, battleReq));
            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }

    }

    private void send(MessageSource messageSource, YiLingMessage.YiLingBattleEnemyExecuteResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}